package com.qst.service;

import com.qst.mapper.ClassMapper;
import com.qst.mapper.UserMapper;
import com.qst.pojo.Choose;
import com.qst.pojo.Result;
import com.qst.pojo.User;
import com.qst.query.ClassQuery;
//import com.qst.query.CourseQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qst.pojo.Class;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassNmae: ClassService
 * @Author: 邓嘉顺
 * @Description:
 */
@Service
public class ClassService {
    @Autowired
    ClassMapper classMapper;
    @Autowired
    UserMapper  userMapper;
    public List<Class> getCourseList(ClassQuery classQuery){

        return classMapper.queryAllCourse(classQuery);
    }

    public Result addChoose(Choose choose) {
        int row =0;
//        User userInfo = (User) session.getAttribute("userInfo");
        Choose choose1=null;
        //非空校验
        Integer userid=choose.getUserId();

        User user = userMapper.queryAllById(userid);

        Class aClass = queryAllById(choose.getClassId());
        Integer classHour = aClass.getClassHour();
//        只拿第一次
        Integer cardHour = user.getCardHour();
        Integer nextHour=cardHour-classHour;
//        if(nextHour>=0){
//            updateNextHour(nextHour,userInfo.getId());
//        }

//        Choose choose1 = classMapper.queryChooseByAll(choose.getUserId(),choose.getClassId());

        String id = userMapper.getCardById(choose.getUserId());
//不是会员
        if(id==null||id==""){

            return new Result(300,"您当前不是会员");

        }
//是会员,查出有没有选过此课
        if(id!=null||id!=""){

             choose1 = classMapper.queryChooseByAll(choose.getUserId(),choose.getClassId());
        }

//没有选择此课,判断剩余课时够不够选择

        if(choose1==null){

            if(nextHour>=0){
                updateNextHour(nextHour,user.getId());
                row = classMapper.addChoose(choose);
            }
            if(nextHour<0){
                return new Result(400,"课时不够,请充值");
            }
//            row = classMapper.addChoose(choose);
        }
//选择过此课
        if(choose1!=null){
            return new Result(100,"已经选择此课程");
        }


        if(row!=1){

            return new Result(100,"添加失败");

        }


        //用户名密码正确，登陆成功
        return new Result(200,"添加成功");
    }

    public List<Class> queryMyClass(Integer id,String className) {
        return classMapper.queryMyClass(id,className);
    }

    public Result delChoose(Choose choose) {

        Integer row =classMapper.delChoose(choose);

        if(row!=1){
            return  new Result(100,"异常情况");
        }
        return new Result(200,"删除成功");
    }

    public Class queryAllById(Integer id) {
        return  classMapper.queryAllById(id);
    }

    public void updateNextHour(Integer nextHour,Integer id) {
        classMapper.updateNextHour(nextHour,id);
    }

    public List<Class> queryAllAdminClass(String className) {

        return classMapper.queryAllAdminClass(className);
    }

    public int getAllCount() {
        return classMapper.getAllCount();
    }

    public Result addAdminClass(Class class1) {

        int  row = classMapper.addAdminClass(class1);

        if(row!=1){
            return new Result(100,"添加失败");
        }
        return new Result(200,"添加成功");
    }


    public Result delBatch(Integer[] ids) {
        if(ids==null || ids.length==0){
            return new Result(100,"至少选中一条记录");
        }
        int row=classMapper.delBatch(ids);
//        有多少id就影响多少行,删除失败
        if(row!=ids.length){
            return  new Result(100,"异常情况");
        }
//        删除成功
        return new Result(200,"删除成功");
    }

    public Result EditAdminClass(Integer id, String field, String value) {
        int row = classMapper.EditAdminClass(id,field,value);

        if(row!=1){

            return new Result(100,"更新失败");

        }

        return new Result(200,"更新成功");
    }

//    public String getCardByid(Integer id) {
//        return  userMapper.getCardByid(id);
//    }
}
